<?php
/**
 * Created by PhpStorm.
 * User: zhaoye
 * Date: 16/7/15
 * Time: 下午3:58
 */

namespace controller\Api;

use MongoDB\BSON\ObjectID;
use ZPHP\Controller\Controller;
use ZPHP\Core\Db;
use ZPHP\Core\Log;

class Test extends Controller{
    public $isApi = true;

    public function init()
    {
        return parent::init();
    }

    public function test()
    {

//        $t_id = yield DB::table('shop','test')->startTrans();
//
//        $res1 = yield DB::table('shop','test')->setTransId($t_id)->add(['name'=>'xiaomeng','number'=>5]);
//        $res2 = yield DB::table('shop_order','test')->setTransId($t_id)->where(['id'=>1])->setDec('number',1);
//        if($res1 && $res2)
//        {
//            yield Db::table('shop','test')->setTransId($t_id)->commit();
//        }
//        else
//        {
//            yield Db::table('shop','test')->setTransId($t_id)->rollback();
//        }
//
//        return 'success';
//        $key = mt_rand(1000,9999);
////        yield Db::table('shop','test')->add(['name'=>'xiaomeng','number'=>$key]);
//        $data = [
//            'class' => 'service/Test', //执行异步任务的类名
//            'method' => 'test',  //执行异步任务中类的方法名
//            'param' => [$key], //类中方法的参数
//        ];
//        Db::task()->call($data);//只是发送任务，不需要获取任务的结果
////        $taskResult = yield Db::task()->callCoroutine($data);
            $id = new ObjectID();
            $id = (string)$id;
        $res = yield Db::collection('test')->add(['_id'=>$id,'name'=>'kkkk']);
        return json_encode($res);
    }



}
